package ar.edu.sovietrussia.customcomponents.librarianstuff;

import java.util.Map;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.book.BookService;
import users.Person;
import ar.edu.sovietrussia.customcomponents.abm.book.AbmBookInfoPanel;
import ar.edu.sovietrussia.customcomponents.loanhandling.SearchCurrentLoansByUserComponent;
import ar.edu.sovietrussia.customcomponents.login.LoginPanel;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainLibrarianComponent;
import ar.edu.sovietrussia.customcomponents.useradmin.UserABMRootComponent;
import ar.edu.sovietrussia.customcomponents.util.IDoReplaces;
import ar.edu.sovietrussia.customcomponents.util.IFeedbacker;
import ar.edu.sovietrussia.customcomponents.util.WrapperPanel;
import dtos.BookInfoDTO;

@AuthorizeInstantiation("ADMIN")
public abstract class LibrarianActionsComponent extends Panel implements IFeedbacker, IDoReplaces {

    private static final long serialVersionUID = 5782642735520738464L;

    @SpringBean
    private BookService bookService;

    public BookService getBookService() {
        return this.bookService;
    }

    public void setBookService(final BookService bookService) {
        this.bookService = bookService;
    }

    public LibrarianActionsComponent(final String id) {
        super(id);

        Person aModel = (Person) this.getSession().getAttribute("model");

        Form<String> form = new Form<String>("librarianStuffForm");

        form.add(new Label("LibrarianNameLabel", aModel.getUserAccount().getRole().toString() + ": "
                + aModel.getSurname() + "," + aModel.getName()));

        // Button to ReservationsList Window
        // form.add(new Button("createLoan_button") {
        // private static final long serialVersionUID = 3610707962001534656L;
        //
        // @Override
        // public void onSubmit() {
        //
        // }
        // });
        // Button to ReservationsList Window
        form.add(new Button("finishLoan_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                LibrarianActionsComponent.this.replaceWith(new SearchCurrentLoansByUserComponent(
                        LibrarianActionsComponent.this.getId(), LibrarianActionsComponent.class));
            }
        });
        // Button to ReservationsList Window
        form.add(new Button("bookAdministration_button") {
            private static final long serialVersionUID = -7180926749213514766L;

            @Override
            public void onSubmit() {
                LibrarianActionsComponent.this.replaceWith(new AbmBookInfoPanel(LibrarianActionsComponent.this.getId()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public FeedbackPanel getFeedbackTarget() {
                        return LibrarianActionsComponent.this.getFeedbackTarget();
                    }

                    @Override
                    public void onSubmitBook(final BookInfoDTO book) {
                        LibrarianActionsComponent.this.getBookService().addBookInfoDTO(book);
                        LibrarianActionsComponent.this.getFeedbackTarget().info("Libro agregado");
                        this.replaceWith(new LoginPanel(this.getId()) {

                            private static final long serialVersionUID = 1L;

                            @Override
                            public Map<String, WrapperPanel> getReplaceableComponents() {
                                return LibrarianActionsComponent.this.getReplaceableComponents();
                            }

                            @Override
                            public FeedbackPanel getFeedbackTarget() {
                                return LibrarianActionsComponent.this.getFeedbackTarget();
                            }
                        });
                    }

                    @Override
                    public Map<String, WrapperPanel> getReplaceableComponents() {
                        return LibrarianActionsComponent.this.getReplaceableComponents();
                    }

                });

            }
        });

        form.add(new Button("userAdministration_button") {
            private static final long serialVersionUID = -7180926749213514766L;

            @Override
            public void onSubmit() {
                LibrarianActionsComponent.this.replaceWith(new UserABMRootComponent(LibrarianActionsComponent.this
                        .getId()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public FeedbackPanel getFeedbackTarget() {
                        return LibrarianActionsComponent.this.getFeedbackTarget();
                    }

                    @Override
                    public Map<String, WrapperPanel> getReplaceableComponents() {
                        return LibrarianActionsComponent.this.getReplaceableComponents();
                    }

                });
            }
        });

        form.add(new Button("backToLibrarianMenu_button") {
            private static final long serialVersionUID = -7180926749213514766L;

            @Override
            public void onSubmit() {
                LibrarianActionsComponent.this.replaceWith(new MainLibrarianComponent(LibrarianActionsComponent.this
                        .getId()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public FeedbackPanel getFeedbackTarget() {
                        return LibrarianActionsComponent.this.getFeedbackTarget();
                    }

                    @Override
                    public Map<String, WrapperPanel> getReplaceableComponents() {
                        return LibrarianActionsComponent.this.getReplaceableComponents();
                    }

                });
            }
        });

        this.add(form);
    }
}